﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class UpgradeServiceStationNeedConfirm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            ssReceiveLabel.Text = DateTime.Now.ToString("yyyy-MM-dd");
            UpgradeService service = new UpgradeService();
            UpgradeForm upForm = service.GetProductDetailForm(long.Parse(Request.QueryString[Constants.QUERYSTR_UPGRADE_ID]));
            productTypeLabel.Text = upForm.ApplicationProduct.ProductType.ProductTypeName;
            serialNumberLabel.Text = upForm.ApplicationProduct.ProductSerialNumber;
            if (!upForm.ApplicationProduct.ApplicationFormReference.IsLoaded)
            {
                upForm.ApplicationProduct.ApplicationFormReference.Load();
            }
            if (!upForm.ApplicationProduct.ApplicationForm.UserReference.IsLoaded)
            {
                upForm.ApplicationProduct.ApplicationForm.UserReference.Load();
            }
            rmaLabel.Text = upForm.ApplicationProduct.ApplicationForm.ApplicationCode;
            clientLabel.Text = upForm.ApplicationProduct.ApplicationForm.User.FullName;

            if (!upForm.ApplicationProduct.QuatationForm.IsLoaded)
            {
                upForm.ApplicationProduct.QuatationForm.Load();
            }

            repaireAccessoryLabel.Text = upForm.ApplicationProduct.RepairAccessory;
            double cost = 0;
            foreach (QuatationForm form in upForm.ApplicationProduct.QuatationForm)
            {
                cost += form.Cost;
            }
            costLabel.Text = cost.ToString();
        }

        protected void confirmLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                UpgradeService service = new UpgradeService();
                UpgradeForm upForm = service.GetProductDetailForm(long.Parse(Request.QueryString[Constants.QUERYSTR_UPGRADE_ID]));
                upForm.Status = "升级-平台收货确认";
                upForm.CompleteTime = DateTime.Now;
                upForm.ServiceStationReceiveRemark = remarkTextBox.Text;
                service.Update(upForm);

                if (!upForm.ApplicationProductReference.IsLoaded)
                {
                    upForm.ApplicationProductReference.Load();
                }
                if (!upForm.ApplicationProduct.UserReference.IsLoaded)
                {
                    upForm.ApplicationProduct.UserReference.Load();
                }
                if (!upForm.UserReference.IsLoaded)
                {
                    upForm.UserReference.Load();
                }
                upForm.ApplicationProduct.User = upForm.User;
                ApplicationProductService productService = new ApplicationProductService();
                productService.UpdateProduct(upForm.ApplicationProduct);

                // 2013.9.8 注释
                //ApplicationProductService productService = new ApplicationProductService();
                //productService.CompleteApplicationProductRepair(upForm.ApplicationProduct.ApplicationProductID, "UpgradeServiceStationNeedConfirm.aspx::confirmLinkButton_Click");

                // 2013.8.31 注释
                //upForm.ApplicationProduct.Status = "维修完毕待发货";
                //upForm.ApplicationProduct.RepairCompleteDateTime = DateTime.Now;
                //service.Update(upForm);
                //ApplicationProductService productservice = new ApplicationProductService();
                //productservice.FinishMaintainProduct(upForm.ApplicationProduct);

                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('平台收货成功');location.href='UpgradeListServiceStationNeedConfirm.aspx';", true);
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "UpgradeServiceStationNeedConfirm.aspx::confirmLinkButton_Click");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('提交过程中发生错误，请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }
    }
}
